package com.example.controller;

import com.example.entity.Test;
import com.example.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 控制类
 * @author 李平
 * @Date 2022-4-30
 */
@RestController
public class TestController {

    @Autowired
    private TestService testService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * mysql数据库测试
     * @return List<Test>
     */
    @GetMapping
    @RequestMapping("/getAll")
    public List<Test> getAll(){
        return testService.getAll();
    }

    /**
     * mysql数据库测试
     * @return List<Test>
     */
    @GetMapping
    @RequestMapping("/getTest")
    public String getTest(){
        return "test";
    }

    /**
     * 阿里云Redis测试
     * @param name
     * @return String
     */
    @GetMapping
    @RequestMapping("/getRedis/{name}")
    public String getSetRedis(@PathVariable("name") String name){
        // 将name值存入Redis数据库中，并设置300秒后过期，自动删除
        redisTemplate.opsForValue().set("name",name,300,TimeUnit.SECONDS);
        return redisTemplate.opsForValue().get("name");
    }

}
